import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
} from '@nestjs/common';
import { BookInfoService } from './book-info.service';
import { CreateBookInfoDto } from './dto/create-book-info.dto';
import { UpdateBookInfoDto } from './dto/update-book-info.dto';
import { OpenCC } from 'opencc';

@Controller('book-info')
export class BookInfoController {
  private converterS2T: OpenCC;
  private converterT2S: OpenCC;

  constructor(private readonly bookInfoService: BookInfoService) {
    this.converterS2T = new OpenCC('s2t.json');
    this.converterT2S = new OpenCC('t2s.json');
  }

  @Post()
  create(@Body() createBookInfoDto: CreateBookInfoDto) {
    return this.bookInfoService.create(createBookInfoDto);
  }

  /**
   * findManyFromMatchFlag
   * @param matchFlag {string}
   * @param info {string}
   */
  @Get(':matchFlag/:info')
  findManyFromMatchFlag(
    @Param('matchFlag') matchFlag: string,
    @Param('info') info: string,
  ) {
    const convertedInfo: string = this.converterS2T.convertSync(info);
    // console.log(convertedInfo);
    return this.bookInfoService.findManyFromMatchFlagAndInfo(
      matchFlag,
      info,
      convertedInfo,
    );
  }

  @Get()
  findAll() {
    return this.bookInfoService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.bookInfoService.findOne(+id);
  }

  @Patch(':id')
  update(
    @Param('id') id: string,
    @Body() updateBookInfoDto: UpdateBookInfoDto,
  ) {
    return this.bookInfoService.update(+id, updateBookInfoDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.bookInfoService.remove(+id);
  }

  /**
   * translate2SimplifiedText
   */
  @Get('utils/translate/:text')
  translate2SimplifiedText(@Param('text') text: string) {
    const convertedInfo: string = this.converterT2S.convertSync(text);
    // console.log(convertedInfo);
    return convertedInfo;
  }
}
